﻿using System.Drawing;
using System.Windows.Forms;
using System;

namespace Useful.GUI.HierarchyDataGridView
{
    public class DataGridViewStretchTextBoxCell : DataGridViewTextBoxCell
    {
        public int begin = 0;
        public int end = 0;

        public DataGridViewStretchTextBoxCell()
        {
        }

        public DataGridViewStretchTextBoxCell(string text, int begin, int end)
        {
            this.Value = text;
            this.begin = begin;
            this.end = end;
        }

        private Rectangle GetRectangle()
        {
            Rectangle cb = this.DataGridView.GetCellDisplayRectangle(begin, RowIndex, false);
            Rectangle ce = this.DataGridView.GetCellDisplayRectangle(end, RowIndex, false);

            Rectangle rect = new Rectangle(cb.Left, cb.Top, ce.Right - cb.Left, cb.Height);

            return rect;
        }

        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            System.Diagnostics.Debug.WriteLine(String.Format("DataGridViewStretchTextBoxCell.Paint: RowIndex = {0}, ColumnIndex = {1}", this.RowIndex, this.ColumnIndex));

            int width = GetWidth();

            Rectangle rect = new Rectangle(cellBounds.X, cellBounds.Y, width, cellBounds.Height);

            base.Paint(graphics, clipBounds, rect, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

            PaintBorder(graphics, clipBounds, rect, cellStyle, advancedBorderStyle);
        }

        public int GetWidth()
        {
            int width = 0;

            for (int i = begin; i <= end; i++)
            {
                if(DataGridView.Columns[i].Visible)
                    width += DataGridView.Columns[i].Width;
            }

            return width;
        }

        public override void PositionEditingControl(bool setLocation, bool setSize, Rectangle cellBounds, Rectangle cellClip, DataGridViewCellStyle cellStyle, bool singleVerticalBorderAdded, bool singleHorizontalBorderAdded, bool isFirstDisplayedColumn, bool isFirstDisplayedRow)
        {
            base.PositionEditingControl(setLocation, setSize, GetRectangle(), GetRectangle(), cellStyle, singleVerticalBorderAdded, singleHorizontalBorderAdded, isFirstDisplayedColumn, isFirstDisplayedRow);
        }

        public void PaintCell(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            Paint(graphics, clipBounds, cellBounds, RowIndex, State, Value, FormattedValue, ErrorText, InheritedStyle, advancedBorderStyle, paintParts);
        }

        protected override void PaintBorder(Graphics graphics, Rectangle clipBounds, Rectangle bounds, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle)
        {
            System.Diagnostics.Debug.WriteLine(String.Format("DataGridViewStretchTextBoxCell.PaintBorder: RowIndex = {0}, ColumnIndex = {1}", this.RowIndex, this.ColumnIndex));

            base.PaintBorder(graphics, clipBounds, bounds, cellStyle, advancedBorderStyle);

            int w = DataGridView.Columns[end].DividerWidth;

            if (w != 0)
            {
                using (Pen p = new Pen(DataGridView.GridColor, w))
                {
                    graphics.DrawLine(p, bounds.Right - w, bounds.Y, bounds.Right - w, bounds.Bottom);
                }
            }
        }
    }
}
